<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/12/0012
 * Time: 13:59
 */

namespace app\sports_admin\model;


use think\Model;

class DeviceLog extends Model
{
    protected $table = 'device_log';

    protected $pk = 'id';

    protected $autoWriteTimestamp = true;

    /**
     * 获取设备绑定记录信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $start 检索的开始时间
     * @param $end 检索的结束时间
    */
    public static function getBindingRecord($page,$rows,$sort,$sortOrder,$searchName,$start,$end)
    {
        $result = self::where('status',1)->where(function($query)use($searchName,$start,$end){
            if($searchName){
                $query->where('number|school_name|parent_phone','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
        })->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->select();

        $count = self::where('status',1)->where(function($query)use($searchName,$start,$end){
            if($searchName){
                $query->where('number|school_name|parent_phone','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
        })->count();
        return json(['rows'=>$result,'total'=>$count]);
    }
    /**
     * 获取设备解绑记录信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $start 检索的开始时间
     * @param $end 检索的结束时间
     */
    public static function getUntyingRecord($page,$rows,$sort,$sortOrder,$searchName,$start,$end)
    {
        $result = self::where('status',2)->where(function($query)use($searchName,$start,$end){
            if($searchName){
                $query->where('number|school_name|parent_phone','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
        })->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->select();

        $count = self::where('status',2)->where(function($query)use($searchName,$start,$end){
            if($searchName){
                $query->where('number|school_name|parent_phone','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
        })->count();
        return json(['rows'=>$result,'total'=>$count]);
    }
}